var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    plain: false,
    disabled: false,//按钮一旦点击提交 则不能操作

    zpmc:'',//作品名称
    content:'',//发布作品内容
    imgs: [],
    upurls: [],
    usid:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var usid = wx.getStorageSync('usid');
    if (usid == "") {
      wx.redirectTo({
        url: '../../pages/index/index'
      })
    } else {
      this.setData({
        usid: usid
      })
    }
  },

  zpmcinput(e) {
    this.setData({
      zpmc: e.detail.value
    })
  },

  contentinput(e) {
    this.setData({
      content: e.detail.value
    })
  },


  //上传图片
  uploadimage: function () {
    var that = this;
    wx.chooseImage({
      count: 4, //张数， 默认9
      sizeType: ['compressed'], //建议压缩图
      sourceType: ['album', 'camera'], // 来源是相册、相机
      success: function (res) {
        for (var i = 0; i < res.tempFilePaths.length; i++) {
          that.imgSecCheck(res.tempFilePaths[i]);
        }
      }
    });
  },


  //.................图片检测........................
  imgSecCheck (imgitem) {
    var that=this;
    wx.showLoading({
      title: '图片检测中',
    })
    const uploadTask = app.wxupload({
      url: 'upload/imgSecCheck.html',
      filePath: imgitem,
      name: 'file',
      formData: {},
      success: function (res) {
        var datas = JSON.parse(res.data);
        wx.hideLoading();
        if(!datas.result){
          wx.showModal({
            title: '失败',
            content:datas.msg,
            showCancel: !1,
          });
          return;
        }else{
          var imgs = that.data.imgs;
          imgs.push(imgitem);
          that.setData({
            imgs: imgs
          })
        }
      }
    })
  },

  //删除图片
  delImage: function (e) {
    var that = this;
    var imgs = that.data.imgs;
    var cuindex = e.target.dataset.index;
    imgs.splice(cuindex, 1);
    that.setData({
      imgs: imgs
    })
  },

  //预览图片
  viewimage: function (e) {
    var cuindex = e.target.dataset.index;
    var that = this;
    var urls = that.data.imgs;
    wx.previewImage({
      current: urls[cuindex], // 当前显示图片的http链接
      urls: urls // 需要预览的图片http链接列表
    })
  },



  sureBookxsyc: function (e) {
    var that = this;
    var zpmc = that.data.zpmc;
    if (zpmc == '') {
      wx.showModal({
        title: '友情提示',
        content: '请填写作品名称!',
        showCancel: !1,
      })
      return;
    }

    var content = that.data.content;
    var imgs = this.data.imgs;
    if (content == '' && imgs.length<=0) {
      wx.showModal({
        title: '友情提示',
        content: '原创作品内容与文章图片不能都为空，请填写作品内容或上传文章图片!',
        showCancel: !1,
      })
      return;
    }
    
    wx.showModal({
      title: '提示',
      content: '是否发布作品,如果仅仅保存不需要发布，点击取消，否则点击确定',
      success(res) {
        wx.showLoading({
          title: '保存中',
        })
        that.setData({
          disabled: true,
          loading: true
        })
        if (res.confirm) {
          that.setData({
            bstate: "1"
          })
        } else if (res.cancel) {
          that.setData({
            bstate: "0"
          })
        }
        //先上传图片
        if (imgs.length > 0) {
          //先上传图片
          that.uploadBookurl();
        } else {
          that.saveBookxsyc();
        }
      }
    })

  },

 

  //提交原创作品图片
  uploadBookurl: function () {
    var that = this;
    var imgs = this.data.imgs;
    var upurls = this.data.upurls;
    const uploadTask =  app.wxupload({
      url: 'upload/uploadwx.html?savefile=xsyc',
      filePath: imgs[0],
      name: 'file',
      formData: {},
      success: function (res) {
        var img = JSON.parse(res.data);
        var url = img.urls;
        upurls.push(url);
        that.setData({
          upurls: upurls
        });
        that.data.imgs.splice(0, 1);
        if (that.data.imgs.length > 0) {
          that.uploadBookurl();
        } else {
          that.saveBookxsyc();
        }
      }
    })
  },


  saveBookxsyc() {
    var that = this;
    var usid = that.data.usid;
    var username = wx.getStorageSync('name');
    var nj = wx.getStorageSync('nj');
    var bj = wx.getStorageSync('bj');
    var zpmc = that.data.zpmc;
    var content = that.data.content;
    var bstate = that.data.bstate;
    var upurls = that.data.upurls;
    
    app.wxpost({
      url: 'wxsyc/addBookxsyc.html',
      data: {
        usid: usid,
        username: username,
        nj: nj,
        bj: bj,
        zpmc: zpmc,
        content: content,
        bstate: bstate,
        upurls: JSON.stringify(upurls)
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.result) {
          wx.showModal({
            title: '成功',
            content: res.data.msg,
            showCancel: !1,
          });
          wx.redirectTo({
            url: '../../pages/xsyc/xsyc'
          })
        } else {
          that.setData({
            disabled: false,
            loading: false
          })
          wx.showModal({
            title: '失败',
            content: res.data.msg,
            showCancel: !1,
          });
        }
      }
    })
  },


})